/*
 * 把task01，和task02，封装在一个类里面，作为两个函数。如下：
  class PrintChar{}
  var  p = new PrintChar(“A”, 10);
  p.for1();  // 第一个作业的结果
  p.for2();  // 第二个作业的结果
*/

const log = console.log;
//类的语法糖
class PrintChar {
  constructor(char, count) {
    this.char = char;
    this.count = count;
  }
  for1() {
    let pChar = this.char;
    for (let i = 0; i < this.count; i++) {
      log(pChar);
      pChar += this.char;
    }
  }
  for2 () {
    let pChar = this.char;
    for (let i = this.count; i > 0; i--) {
      pChar = this.char;
      for (let j = 0; j < i - 1; j++) {
        pChar += this.char;
      }
      log(pChar);
    }
  }
}
//实例
var p = new PrintChar("A", 10);
p.for1();
log("--------------");
p.for2();
log("~~~~~~~~~~~~~~");

// 传统基于函数创建类
function PrintChar2(char = "A", count = 10) {
  this.char = char;
  this.count = count;
  /*
  let pChar = char;
  let pCount = count;
  this.for1 = function() {
    for (let i = 0; i < pCount; i++) {
      log(pChar);
      pChar += char;
    }
  };
  this.for2 = function() {
    for (let i = pCount; i > 0; i--) {
      pChar = char;
      for (let j = 0; j < i - 1; j++) {
        pChar += char;
      }
      log(pChar);
    }
  }
  */
}
PrintChar2.prototype.for1 = function() {
  let pChar = this.char;
  for (let i = 0; i < this.count; i++) {
    log(pChar);
    pChar += this.char;
  }
}
PrintChar2.prototype.for2 = function() {
  let pChar = this.char;
  for (let i = this.count; i > 0; i--) {
    pChar = this.char;
    for (let j = 0; j < i - 1; j++) {
      pChar += this.char;
    }
    log(pChar);
  }
}
//实例
var p1 = new PrintChar2("B", 10);
p1.for1();
log("--------------");
p1.for2();
